/*jslint sub: true */
/*global Class */

/**
 * Класс формы.
 *
 * @class
 * @name Form
 * @extends Class
 */
var Form = Class.$extend({

    /**
     * Список элементов формы.
     *
     * @private
     * @field
     * @name Form#elements
     * @type {Form.Group[]|Form.Item[]}
     */
    elements : null,

    /**
     * Название формы.
     *
     * @private
     * @field
     * @name Form#name
     * @see Form#getName
     * @type {String}
     */
    name : null,

    /**
     * Конструктор.
     *
     * @public
     * @function
     * @name Form#$init
     * @return {void}
     */
    $init : function () {
        'use strict';
        this.elements = [];
    },

    /**
     * Получить название формы.
     *
     * @public
     * @function
     * @name Form#getName
     * @see Form#name
     * @return {String}
     */
    getName : function () {
        'use strict';
        return String(this.name || '');
    },

    /**
     * Добавить группу.
     *
     * @public
     * @function
     * @name Form#addGroup
     * @see Form#addItem
     * @param {Form.Group} group
     * @return {void}
     */
    addGroup : function (group) {
        'use strict';
        if (!(group instanceof Form.Group)) {
            throw new TypeError('');
        }
        this.elements.push(group);
    },

    /**
     * Добавить элемент ввода.
     *
     * @public
     * @function
     * @name Form#addItem
     * @see Form#addGroup
     * @param {Form.Item} item
     * @return {void}
     */
    addItem : function (item) {
        'use strict';
        if (!(item instanceof Form.Item)) {
            throw new TypeError('');
        }
        this.elements.push(item);
    },

    /**
     * Проверить, валидна ли форма.
     *
     * @public
     * @function
     * @name Form#isValid
     * @return {Boolean}
     */
    isValid : function () {
        'use strict';
        var result = true;
        this.elements.forEach(function (element) {
            result = result && element.isValid();
        });
        return result;
    },

    getMessages : function () {
        'use strict';
    },

    getValues : function () {
        'use strict';
    }

});

/**
 * @class
 * @name Form.Group
 * @extends Class
 */
Form.Group = Class.$extend({

    /**
     * Список элементов группы.
     *
     * @private
     * @field
     * @name Form.Group#elements
     * @type {Form.Group[]|Form.Item[]}
     */
    elements : null,

    /**
     * Заголовок группы.
     *
     * @private
     * @field
     * @name Form.Group#label
     * @type {String}
     */
    label : null,

    /**
     * Подсказка группы.
     *
     * @private
     * @field
     * @name Form.Group#help
     * @type {String}
     */
    help : null,

    /**
     * Конструктор.
     *
     * @public
     * @function
     * @name Form.Group#$init
     * @param {String} [label]
     * @param {String} [help]
     * @return {void}
     */
    $init : function (label, help) {
        'use strict';
        // Заголовок группы.
        if (typeof label === 'string') {
            this.label = label;
        }
        // Подсказка группы.
        if (typeof help === 'string') {
            this.help = help;
        }
    },

    /**
     * @public
     * @function
     * @name Form.Group#addGroup
     * @see Form.Group#addItem
     * @param {Form.Group} group
     * @return {void}
     */
    addGroup : function (group) {
        'use strict';
        if (!(group instanceof Form.Group)) {
            throw new TypeError('');
        }
        this.elements.push(group);
    },

    /**
     * @public
     * @function
     * @name Form.Group#addItem
     * @see Form.Group#addGroup
     * @param {Form.Item} item
     * @return {void}
     */
    addItem : function (item) {
        'use strict';
        if (!(item instanceof Form.Item)) {
            throw new TypeError('');
        }
        this.elements.push(item);
    },

    /**
     * @public
     * @function
     * @name Form.Group#isValid
     * @return {Boolean}
     */
    isValid : function () {
        'use strict';
        var result = true;
        this.elements.forEach(function (element) {
            result = result && element.isValid();
        });
        return result;
    },

    /**
     * @public
     * @function
     * @name Form.Group#getLabel
     * @see Form.Group#getHelp
     * @return {String}
     */
    getLabel : function () {
        'use strict';
        return String(this.label || '');
    },

    /**
     * @public
     * @function
     * @name Form.Group#getHelp
     * @see Form.Group#getLabel
     * @return {String}
     */
    getHelp : function () {
        'use strict';
        return String(this.help || '');
    },

    getValues : function () {
        'use strict';
    }


});

/**
 * @class
 * @name Form.Item
 * @extends Class
 */
Form.Item = Class.$extend({

    require : null,

    isRequire : function () {
        'use strict';
        return !!this.require;
    },

    /**
     * @param parent
     * @param {Object} options
     */
    $init : function (parent, options) {
        'use strict';
        options = options || {};
        if (typeof options['require'] === 'string') {
            this.require = ['true', 'yes', 'on', '1'].indexOf(options['require'].toLowerCase()) !== -1;
        } else if (typeof options['require'] === 'number') {
            this.require = options['require'] > 0;
        } else {
            this.require = !!options['require'];
        }
    },

    /**
     * @public
     * @function
     * @name Form.Item#isValid
     * @return {Boolean}
     */
    isValid : function () {
        'use strict';
        return false;
    },


    getValue : function () {
        'use strict';
        return null;
    }

});

Form.Text = Form.Item.$extend({

    $init : function (options) {

    }

});

Form.Checkbox = Form.Item.$extend({});

Form.Radio = Form.Item.$extend({});



